#region License
/*
 * Copyright (c) 2007, Matthew Holmes
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * * Neither the name of The Mango Project nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Mango.Framework.Common {
    public class MonitoredList<T> : IList<T> {
        #region Events

        public event EventHandler<EventArgs> ListChanged;

        #endregion

        #region Fields

        private List<T> m_List = null;

        #endregion

        #region Properties

        private List<T> List {
            get {
                if(m_List == null) {
                    m_List = new List<T> ();
                }

                return m_List;
            }
        }

        #endregion

        #region Public Methods

        public void AddRange (IEnumerable<T> source) {
            List.AddRange (source);
            OnListChanged ();
        }

        #endregion

        #region Private Methods

        private void OnListChanged() {
            if(ListChanged != null) {
                ListChanged (this, new EventArgs ());
            }
        }

        #endregion

        #region IList<T> Members

        public int IndexOf (T item) {
            return List.IndexOf (item);
        }

        public void Insert (int index, T item) {
            List.Insert (index, item);
            OnListChanged ();
        }

        public void RemoveAt (int index) {
            List.RemoveAt (index);
            OnListChanged ();
        }

        public T this[int index] {
            get {
                return List[index];
            }
            set {
                List[index] = value;
                OnListChanged ();
            }
        }

        #endregion

        #region ICollection<T> Members

        public void Add (T item) {
            List.Add (item);
            OnListChanged ();
        }

        public void Clear () {
            List.Clear ();
            OnListChanged ();
        }

        public bool Contains (T item) {
            return List.Contains (item);
        }

        public void CopyTo (T[] array, int arrayIndex) {
            List.CopyTo (array, arrayIndex);
        }

        public int Count {
            get {
                return List.Count;
            }
        }

        public bool IsReadOnly {
            get {
                return false;
            }
        }

        public bool Remove (T item) {
            bool flag = List.Remove (item);
            OnListChanged ();
            return flag;
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator () {
            return List.GetEnumerator ();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {
            return List.GetEnumerator ();
        }

        #endregion
    }
}
    